﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

namespace JohnnySins
{
    public partial class DownloadArchive : System.Web.UI.Page
    {
        protected string PathChat
        {
            get
            {
                return MapPath("~/Files/Archives/Chat/");
            }
        }
        protected string PathVideo
        {
            get
            {
                return MapPath("~/Files/Archives/Stream/");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();

            string path = "";
            if (FileType == 1)
            {
                path = Path.Combine(PathVideo, FileName);
                //Response.ContentType = "video/x-flv";
            }
            else
            {
                path = Path.Combine(PathChat, FileName);
                //Response.ContentType = "text/plain";
            }

            string fn = FileName;
            if (fn.StartsWith("_")) fn = fn.Remove(0, 1);

            Download.LargeFile(fn, path, "application/octet-stream");
            Response.End();
        }

        public int FileType
        {
            get
            {
                int result = -1;
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    if (int.TryParse(Request.QueryString["t"], out result))
                        return result;
                }

                return result;
            }
        }

        public string FileName
        {
            get
            {
                string result = "";
                if (!string.IsNullOrEmpty(Request.QueryString["f"]))
                {
                    result = Server.UrlDecode(Request.QueryString["f"]);
                }

                return result;
            }
        }
    }
}